
import Mock from 'mockjs'
var teacher = ['小飞侠', '王露纯', '雷承徳', 'Kerwin', '前端小灰狼']
var grade = ['[初级]', '[中级]', '[中级]', '[初级]', '[高级]', '[高级]']
var classname = ['HTML5大前端', 'JAVA开发', 'Python数据分析', '云计算', '全栈软件测试', '大数据', '智能互联网', '网络安全', '认证考试', '区块链']
var list = []
for (var i = 0; i < 100; i++) {
    list.push({
        id: Mock.mock('@guid'),
        curriculumName: classname[Math.round(Math.random() * 9)] + grade[Math.round(Math.random() * 5)],
        number: Mock.mock('@natural(100000, 1000000)'),
        img: `kecheng-picture0${Math.round(Math.random() * 7 + 1)}.png`,
        price: Mock.mock('@natural(400, 700)'),
        sales: Mock.mock('@natural(100, 500)'),
        name: teacher[Math.round(Math.random() * 4)],
        date: Mock.mock('@date'),
    })
}

export default {
    // 获取分页
    getAllData: config => {
        console.log(config.body)
        var { page, pagesize } = JSON.parse(config.body)
        // console.log(page, pagesize)
        var arr = list.filter((ele, index) => (page - 1) * pagesize <= index && index < page * pagesize)
        // console.log(arr)
        return {
            message: "请求成功",
            data: arr,
            news: list.length
        }
    },
    // 删除
    deleData: config => {
        // console.log(config.body)
        list = list.filter(ele => ele.id != config.body)
        return {
            data: "删除成功666",
        }
    },
    // 搜索
    searchDatas: config => {
        var sum = []
        // console.log(config.body)
        sum = list.filter(ele => ele.name.indexOf(config.body) != -1)
        if (config.body) {
            return {
                data: sum,
                news: sum.length
            }
        }


    },
    // 删除所有
    deleAllnews: config => {
        var allId = []
        var all = JSON.parse(config.body)
        if (config.body) {
            all.forEach(ele => {
                return allId.push(ele.id)
            });
        }
        list = list.filter(ele => allId.indexOf(ele.id) == -1)
        console.log(list, "删除成功")
        // return {
        //     mess: list,
        // }
    },
    // 修改
    updateMess: config => {
        var upId = JSON.parse(config.body).id
        // console.log(upId)
        var index = list.findIndex(ele => ele.id == upId)
        // console.log(index)
        list.splice(index, 1, JSON.parse(config.body))
    },
    searchbyTime: config => {
        var { value1, value2, page, pagesize } = JSON.parse(config.body)
        var arrtime = list.filter(ele => Date.parse(value1) <= Date.parse(ele.date) && Date.parse(ele.date) <= Date.parse(value2))
        // 将时间戳转为时间
        var listtime = arrtime.filter((ele, index) => (page - 1) * pagesize <= index && index < page * pagesize)
        console.log(arrtime)
        if (config.body) {
            return {
                data: listtime,
                mess: arrtime.length
            }

        }
    }

}




